package com.jason.kotlin.constraint

import android.animation.ValueAnimator
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.jason.kotlin.R
import kotlinx.android.synthetic.main.activity_constraint_layer.*

/**
 * Description:ConstraintLayerActivity
 *
 * @author chenbaoyang
 * @create 2020/12/24 16:46
 */
class ConstraintLayerActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_constraint_layer)
        btn.setOnClickListener {
            val anim = ValueAnimator.ofFloat(0f, 360f)
            anim.addUpdateListener { animation ->
                val angle = animation.animatedValue as Float
                layer.rotation = angle
                layer.scaleX = 1 + (180 - Math.abs(angle - 180)) / 30f
                layer.scaleY = 1 + (180 - Math.abs(angle - 180)) / 30f

                var shift_x = 50 * Math.sin(Math.toRadians((angle * 5).toDouble())).toFloat()
                var shift_y = 50 * Math.sin(Math.toRadians((angle * 7).toDouble())).toFloat()
                layer.translationX = shift_x
                layer.translationY = shift_y
            }
            anim.duration = 4000
            anim.start()
        }
    }
}